﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPIDesktopClient.Entities;
using PPIDesktopClient.Logic;
using System.ComponentModel;
using System.Data;
using VMind.Global;

namespace PPIDesktopClient.General
{
    public class LoadServerInformationAsyn
    {
        public List<EUtility_QueryAsset> QueryAssets { get; set; }
        public List<EUtility_Polygon> Polygons { get; set; }
        public List<EUtility_QueryPolygon> QueryPolygons { get; set; }

        protected BackgroundWorker m_ServerInformationWorker;

        public LoadServerInformationAsyn()
        {
            m_ServerInformationWorker = new BackgroundWorker();
            m_ServerInformationWorker.DoWork += new DoWorkEventHandler(m_ServerInformationWorker_DoWork);
            m_ServerInformationWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_ServerInformationWorker_RunWorkerCompleted);
        }

        public void StartDownload()
        {
            m_ServerInformationWorker.RunWorkerAsync();
        }

        protected void LoadQueryAssets()
        {
            QueryAssets = new List<EUtility_QueryAsset>();
            foreach (DataRow _Dr in LUtility_QueryAsset.GetQueryAssets().Rows)
                QueryAssets.Add(new EUtility_QueryAsset(ConvertHelper.ToInt32(_Dr["Utility_QueryAssetId"]), ConvertHelper.Tostring(_Dr["Name"]), new EUtility_Query_Type(ConvertHelper.ToInt32(_Dr["Utility_Query_TypeFk"])), _Dr["Color"].ToString(), (EUtility_QueryAssetTextShow)ConvertHelper.ToInt32(_Dr["TextToShow"])));
        }

        protected void LoadPolygons()
        {
            Polygons = new List<EUtility_Polygon>();
            foreach (DataRow _Dr in LUtility_Polygon.SelectAll().Rows)
            {
                EUtility_Polygon _Polygon = new EUtility_Polygon(ConvertHelper.ToInt32(_Dr["Utility_PolygonId"]), ConvertHelper.Tostring(_Dr["Name"]), new EUtility_Polygon(ConvertHelper.ToInt32(_Dr["Utility_Polygon_ParentPolygonFk"])));
                _Polygon.LineColor = ConvertHelper.ToInt32(_Dr["LineColor"]);
                _Polygon.LineWidth = ConvertHelper.ToInt32(_Dr["LineWidth"]);
                _Polygon.FillColor = ConvertHelper.ToInt32(_Dr["FillColor"]);
                _Polygon.Name = ConvertHelper.Tostring(_Dr["Name"]);
                Polygons.Add(_Polygon);
            }
            QueryPolygons = new List<EUtility_QueryPolygon>();
            foreach (DataRow _Dr in LUtility_QueryPolygon.SelectAll().Rows)
                QueryPolygons.Add(new EUtility_QueryPolygon(ConvertHelper.ToInt32(_Dr["Utility_QueryPolygonId"]), ConvertHelper.Tostring(_Dr["Name"]), new EUtility_Query_Type(ConvertHelper.ToInt32(_Dr["Utility_Query_TypeFk"]))));
        }

        #region events
        public event EventHandler FinishLoadInformation;
        private void OnFinishLoadInformation(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (FinishLoadInformation != null)
            {
                FinishLoadInformation(o, e);
            }
        }

        #endregion


        #region Worker Events

        void m_ServerInformationWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            LoadQueryAssets();
            LoadPolygons();
        }
        
        void m_ServerInformationWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            OnFinishLoadInformation(sender, new EventArgs());
        }
       

        #endregion

    }
}
